package com.jstyle.beatecg.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jstyle.beatecg.R;
import com.jstyle.beatecg.model.PathRecord;
import com.jstyle.beatecg.util.DateUtil;

import java.text.NumberFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/8.
 */

public class SportHistoryAdapter extends RecyclerView.Adapter {


    onItemClickListener clickListener;
   List<PathRecord>pathRecordList;
    public SportHistoryAdapter(List<PathRecord>pathRecordList,onItemClickListener clickListener) {
        this.clickListener=clickListener;
        this.pathRecordList=pathRecordList;
    }

    public void updata(List<PathRecord>pathRecordList) {
        this.pathRecordList=pathRecordList;
        this.notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sport_history, null);
        return new SportHistoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            SportHistoryViewHolder sportHistoryViewHolder=(SportHistoryViewHolder)holder;
        final PathRecord pathRecord=pathRecordList.get(position);
        String date=pathRecord.getMDate();
        float durationTime=Float.valueOf(pathRecord.getMDuration());
        String duration= DateUtil.getSportTime((int) durationTime);
        float distance=Float.valueOf(pathRecord.getDistanceBeat())/100f;

        NumberFormat numberFormat=NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        sportHistoryViewHolder.tvHistoryDate.setText(date);
        sportHistoryViewHolder.tvItemTime.setText(duration);
        sportHistoryViewHolder.tvItemDistance.setText(numberFormat.format(distance)+"km");
        sportHistoryViewHolder.rlItemHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onItemClick(pathRecord.getMId());
            }
        });
        sportHistoryViewHolder.rlItemHistory.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                clickListener.onLongClick(pathRecord.getMId());
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return pathRecordList==null?0:pathRecordList.size();
    }

    class SportHistoryViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageView2)
        public ImageView imageView2;
        @BindView(R.id.tv_history_date)
        public TextView tvHistoryDate;
        @BindView(R.id.tv_item_time)
        public TextView tvItemTime;
        @BindView(R.id.tv_item_distance)
        public TextView tvItemDistance;
        @BindView(R.id.rl_item_history)
        public RelativeLayout rlItemHistory;

        public SportHistoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface onItemClickListener{
        public void onItemClick(long id);
        public void onLongClick(long id);
    }
}
